<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-model-item label="区域名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="areaName">
              <a-input v-model="model.areaName" :maxLength="100" placeholder="请输入区域名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="区域类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="areaType">
              <j-dict-select-tag type="list" v-model="model.areaType" dictCode="area_type" placeholder="请选择区域类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.areaType == 0">
            <a-form-model-item label="班级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classId">
              <j-dict-select-tag type="list" v-model="model.classId" dictCode="sk_class,class_name,id,del_flag = 0 order by sort_no asc" placeholder="请选择班级" allowClear />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="区域位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="areaAdress">
              <a-textarea v-model="model.areaAdress" :maxLength="255" :autosize="{ minRows: 4 }" placeholder="请输入区域位置" />
              <div style="text-align: right;margin:0;line-height: 20px;" v-if="!formDisabled">{{model.areaAdress?model.areaAdress.length :0}}/255</div>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs" v-if="this.model.isOutdoor == '0'">
      <a-tab-pane tab="区域采集时间" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="skAreaAcquisitionTimeTable.loading"
          :columns="skAreaAcquisitionTimeTable.columns"
          :dataSource="skAreaAcquisitionTimeTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true">

          <template v-slot:action="props">
              <a @click="handleDelete(props)">删除</a>
          </template>
        </j-editable-table>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { httpAction,getAction } from '@/api/manage'
  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'
  export default {
    name: 'SkAreaForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
          classId: undefined
        },
        // 新增时子表默认添加几行空数据
        showTables: false,
        addDefaultRowNum: 1,
        validatorRules: {
          areaName:[{required: true, message: '请输入区域名称'}],
          areaType:[{required: true, message: '请选择区域类型'}],
          classId:[{required: true, message: '请选择班级'}],
        },
        refKeys: ['skAreaAcquisitionTime', ],
        tableKeys:['skAreaAcquisitionTime', ],
        activeKey: 'skAreaAcquisitionTime',
        // 区域采集时间
        skAreaAcquisitionTimeTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '区域采集开始时间',
              key: 'acquisitionStartTime',
              type: FormTypes.time,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '区域采集结束时间',
              key: 'acquisitionEndTime',
              type: FormTypes.time,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '操作',
              dataIndex: 'action',
              align:"center",
              fixed:"right",
              width:147,
              scopedSlots: { customRender: 'action' },
            }
          ]
        },
        url: {
          add: "/management/skArea/add",
          edit: "/management/skArea/edit",
          queryById: "/management/skArea/queryById",
          skAreaAcquisitionTime: {
            list: '/management/skArea/querySkAreaAcquisitionTimeByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.skAreaAcquisitionTimeTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.skAreaAcquisitionTime.list, params, this.skAreaAcquisitionTimeTable)
        }
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_NO_PASSED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          skAreaAcquisitionTimeList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
      submitForm () {
        console.log('sss');
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            this.$emit("modelLoading",that.confirmLoading)
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'POST';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.$emit("modelLoading",that.confirmLoading)
            })
          }
         
        })
      },
    }
  }
</script>

<style scoped>
</style>